package com.zhidisoft.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zhidisoft.base.action.BaseAction;
import com.zhidisoft.common.ResultData;
import com.zhidisoft.system.entity.Function;
import com.zhidisoft.system.vo.FunctionVO;

@SuppressWarnings("all")
@Controller
@Scope("prototype")
public class FunctionAction extends BaseAction{
	
	private String ids;
	
	private String id;
	
	/**
	 * 直接转发至list_function.jsp
	 * @return
	 */
	public String list(){
		
		return "list";
	}
	
	/**
	 * 直接转发至edit_function.jsp
	 * @return
	 */
	public String edit(){
		return "edit";
	}
	
	/**
	 * [{id:1,text:"系统权限",children;[{id:2,text:""},{id:3,text""}]}]
	 * @return
	 */
	public String findMenu() throws Exception{
		List<Function> list = funcService.findByParentId(1, id);
		
		List<Map<String,Object>> treeNodes = new ArrayList<Map<String,Object>>();
		for(Function func : list){
			Map<String,Object> node = new HashMap<String, Object>();
			node.put("id", func.getId());
			node.put("text", func.getFuncName());
			if(func.getChildren().size()>0){
				node.put("state", "closed");
			}
			treeNodes.add(node);
		}
		
		resultData = ResultData.buildSuccessResult("获取权限节点成功", treeNodes);
		printJSONObject(resultData);
		return NONE;
	}
	
	
	
	/**
	 * 获取所有的权限信息
	 * @return
	 */
	public String findAll() throws Exception{
		List<FunctionVO> list = funcService.findAll();
		resultData = ResultData.buildSuccessResult("获取所有权限信息成功", list);
		printJSONObject(resultData);
		return NONE;
	}
	
	public String findByRoleIds() throws Exception{
		String[] roleIds = ids.split(",");
		//调用funcService获取当前角色对应 权限信息
		List<FunctionVO> list = funcService.findByRoleIds(roleIds);
		resultData = ResultData.buildSuccessResult("获取角色的权限信息成功", list);
		printJSONObject(resultData);
		
		return NONE;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	
}
